package com.xz.Utils;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import javax.xml.transform.Source;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 * @author zhangh     
 * @version V1.0.0
 * @projectName selfstudy-crawler
 * @title     HttpUtils   
 * @package    com.xz.Utils  
 * @date   2019/10/10 17:40  
 * @explain 封装的http工具类
 */
@Component
public class HttpUtils {

    private PoolingHttpClientConnectionManager cm;

    public HttpUtils() {
        this.cm = new PoolingHttpClientConnectionManager();

        //    设置最大连接数
        cm.setMaxTotal(200);

        //    设置每个主机的并发数
        cm.setDefaultMaxPerRoute(20);
    }


    /**
     * 获取内容
     */
    public String getHtml(String url) {
        // 获取httpclient对象
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();

        // 声明httpget请求
        HttpGet httpGet = new HttpGet(url);
        // 设置请求参数
        setConfig(httpGet);

        CloseableHttpResponse response = null;

        try {
            response = httpClient.execute(httpGet);
            // 解析返回的数据
            if (response.getStatusLine().getStatusCode() == 200) {
                String html = "";
                if (response != null && response.getEntity() != null) {
                    html = EntityUtils.toString(response.getEntity(),"UTF-8");
                }
                return html;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }


    /**
     * 获取图片
     * @param url
     * @return
     */
    public String getImage(String url) {
        // 获取HttpClient对象
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();

        // 声明请求对象
        HttpGet httpGet = new HttpGet(url);
        // 设置请求参数
        setConfig(httpGet);

        CloseableHttpResponse response = null;

        try {
            response = httpClient.execute(httpGet);

            // 解析返回的数据下载图片
            if (response.getStatusLine().getStatusCode() == 200) {

                // 获取文件类型
                String extName  = url.substring(url.lastIndexOf("."));
               // 使用UUID生成图片名
                String imageName = UUID.randomUUID().toString() + extName;
                // 声明输出的文件
                OutputStream outputStream = new FileOutputStream(new File("E:/images/"+imageName));
                // 使用响应体输出文件
                response.getEntity().writeTo(outputStream);

                // 返回生成的图片名
                return imageName;
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }



    /**
     * 获取请求参数对象
     */
    private void setConfig(HttpRequestBase httpRequestBase) {

        //String reqHeaderStr = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3\n" +
        //                "Accept-Encoding: gzip, deflate\n" +
        //                "Accept-Language: zh-CN,zh;q=0.9\n" +
        //                "Cache-Control: no-cache\n" +
        //                "Connection: keep-alive\n" +
        //                "Cookie: __jdu=989280654; shshshfpa=6882226d-4a34-2519-eaac-1d0e50e5bd12-1569374457; unpl=V2_ZzNtbREEFEV0WxNdKxpUVmJQFQ0SXhcVIA4RA30ZWwBgVhteclRCFX0URlVnGV0UZwQZXkZcQRNFCEdkeBBVAWMDE1VGZxBFLV0CFSNGF1wjU00zQwBBQHcJFF0uSgwDYgcaDhFTQEJ2XBVQL0oMDDdRFAhyZ0AVRQhHZHMQVAZuAhVZR2dzEkU4dl1%2bEFoCZzMTbUNnAUEpD05dch5YSG8KGl5LVkQRcDhHZHg%3d; __jdv=76161171|baidu-pinzhuan|t_288551095_baidupinzhuan|cpc|0f3d30c8dba7459bb52f2eb5eba8ac7d_0_bbfa0bd8a29b4b6aa8e1d7ff71646d82|1570695937967; areaId=1; ipLoc-djd=1-2800-0-0; PCSYCityID=CN_110000_110100_110108; shshshfpb=6882226d-4a34-2519-eaac-1d0e50e5bd12-1569374457; xtest=4391.cf6b6759; 3AB9D23F7A4B3C9B=B4K2SXUGB2KNL7M4J5YZXKTS5SPH24AJ4HITW472N3FJNMKZFSJLN3NWJ7R27SCWYKIAO5FLJHJJVNAPR4YQBRLUL4; __jda=122270672.989280654.1569374455.1570695938.1570703384.3; __jdc=122270672; rkv=V0900; __jdb=122270672.2.989280654|3.1570703384; shshshfp=d42d44b6bf387a787b4c6e3f9e646da7; shshshsID=5c86f35b91d212a532f330b03bd72433_2_1570703635981; qrsc=3\n" +
        //                "Host: search.jd.com\n" +
        //                "Pragma: no-cache\n" +
        //                "Upgrade-Insecure-Requests: 1\n" +
        //                "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36";
        //// 设置请求头
        //Map<String, String> headers = MyStrignUtils.parseHeader(reqHeaderStr);
        //if (headers != null && headers.size() > 0) {
        //    Set<String> keys = headers.keySet();
        //    for (String key : keys) {
        //        httpRequestBase.setHeader(key,headers.get(key));
        //    }
        //}

        httpRequestBase.setHeader("User-Agent", "Mozilla/5.0");
        httpRequestBase.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        httpRequestBase.setHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");//"en-US,en;q=0.5");
        httpRequestBase.setHeader("Accept-Charset", "ISO-8859-1,utf-8,gbk,gb2312;q=0.7,*;q=0.7");


        RequestConfig config = RequestConfig.custom().setConnectTimeout(1000)// 设置创建连接的超时时间
                .setConnectionRequestTimeout(500) // 设置获取连接的超时时间
                .setSocketTimeout(10000) // 设置连接的超时时间
                .build();
        httpRequestBase.setConfig(config);
    }
}
